<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css"/>
    <title>科室管理</title>
</head>
<body>
<div class="searchBtn">
    名称:
    <div class="layui-inline">
        <input class="layui-input" name="name" id="name" autocomplete="off">
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<table  id="department" lay-filter="department"></table>
<div id="toolbar" style="display: none; ">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-sm layui-btn-danger " lay-event="del">删除</a>
</script>

<!--添加修改表单，通过拟态框来见实现-->
<div id="modal" style="display:none">
    <form class="layui-form"  lay-filter="entity">
        <input type="text" name="id"  style="display:none">
        <div class="layui-form-item">
            <label class="layui-form-label">科室名称:</label>
            <div class="layui-input-block   ">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入科室名称"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">科室位置:</label>
            <div class="layui-input-block">
                <input type="text" name="address" required="" lay-verify="required" placeholder="请输入科室位置"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">课时归属</label>
            <div class="layui-input-block">
                <select name="type" lay-verify="">
                    <option value="">请选择</option>
                    <option value="10">内科</option>
                    <option value="11">外科</option>
                    <option value="12">急诊</option>
                    <option value="13">其他</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="formDemo">立即提交</button>
                <button style="display: none;" type="reset" class="layui-btn layui-btn-primary form-reset">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/layui/layui.js"></script>
<script>
    layui.use(['table', 'layer', 'form', 'jquery'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;

        table.render({
            elem: '#department'
            ,id:'testReload'
            ,height:525
            , url: '/api/department/page'
            , page: true
            , cols: [[
                {field: "id", title: "ID",sort: true , fixed:'left'},
                {field: "name", title: "名称"},
                {field: "address", title: "位置" , sort: true},
                {field: "type", title: "归属",templet: function (d) {
                        var typeStr;
                        if (d.type==10){
                            typeStr ='内科';
                        }else if (d.type==11){
                            typeStr ='外科';
                        }else if (d.type==12){
                            typeStr ='急诊';
                        }else {
                            typeStr ='其他';
                        }
                        return typeStr;
                    }
                },
                {field: "createDatetime"
                    , title: "创建时间"
                    ,sort:true
                    ,templet:"<div>{{layui.util.toDateString(d.createTime,'yyyy-MM-dd HH:mm:ss')}}</div>"
                },
                {title: '操作', toolbar: '#barDemo', align: 'center'}
            ]]
            , toolbar: '#toolbar'
            , response: {
                statusCode: 1
            },
            parseData: function (rs) {//数据格式解析
                if (rs.status == 1) {
                    return {
                        "code": rs.status,
                        "msg": rs.message,
                        "count": rs.total,
                        "data": rs.data
                    }
                }
            }
        });
        //监听事件
        table.on('toolbar(department)', function (obj) {
            switch (obj.event) {
                case 'add':
                    open2("添加页面", null);
                    break;
            };
        });
        //监听工具条
        table.on('tool(department)', function (obj) {
            var data = obj.data;
                switch (obj.event) {
                    case 'del':
                        layer.confirm('真的删除这条信息吗？', function (index) {
                            $.ajax({
                                url: '/api/department/' + data.id,
                                type: 'delete',
                                dataType: 'json',
                                success: function (res) {
                                    if (da.status == 1) {
                                        layer.msg(res.message)
                                        obj.del();

                                    } else {
                                        layer.msg(res.message)
                                    }
                                },
                            }) //ajax结束
                            layer.close(index);
                        });
                        break;
                    case 'edit':
                        open2("修改页面",data);
                        break;
                }

        });

        //"模态框",
        function open2(title, entity) {
            form.val("entity", entity);
            layer.open({
                type: 1
                , title: title
                , content: $("#modal")
                , offset: '10px'
                , area: ['600px', '80%']
                , end: function () {
                    $('.form-reset').click()
                    active.reload();
                }
            });
        }
        //监听提交
        form.on('submit(formDemo)', function () {
            var data = form.val("entity");
            var type = data.id ? "put" : "post";
            console.log(data);
            $.ajax({
                url: '/api/department',
                type :type,
                dataType: "json",
                contentType: 'application/json',
                data: JSON.stringify(data),
                success: function (da) {
                    if (da.status == 1) {
                        layer.msg(da.message,{time:1*1000},function() {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(da.message);
                    }
                }
            });
            return false;
        });

        var active = {
            reload: function () {
                var name = $('#name');
                table.reload('testReload', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        "name": name.val()
                    }
                });
            }
        };
        $('.searchBtn .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>